﻿using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using HslCommunication.BasicFramework;
using HslCommunication.Core;
using HslCommunication.Core.Address;
using HslCommunication.Core.Net;
using HslCommunication.Reflection;

namespace HslCommunication.Profinet.Siemens
{
	/// <inheritdoc cref="T:HslCommunication.Profinet.Siemens.SiemensPPI" />
	// Token: 0x0200004E RID: 78
	public class SiemensPPIOverTcp : NetworkDeviceSoloBase
	{
		/// <inheritdoc cref="M:HslCommunication.Profinet.Siemens.SiemensPPI.#ctor" />
		// Token: 0x0600063C RID: 1596 RVA: 0x0001DE71 File Offset: 0x0001C071
		public SiemensPPIOverTcp()
		{
			base.WordLength = 2;
			base.ByteTransform = new ReverseBytesTransform();
			this.communicationLock = new object();
		}

		/// <summary>
		/// 使用指定的ip地址和端口号来实例化对象<br />
		/// Instantiate the object with the specified IP address and port number
		/// </summary>
		/// <param name="ipAddress">Ip地址信息</param>
		/// <param name="port">端口号信息</param>
		// Token: 0x0600063D RID: 1597 RVA: 0x0001DEA1 File Offset: 0x0001C0A1
		public SiemensPPIOverTcp(string ipAddress, int port)
		{
			base.WordLength = 2;
			this.IpAddress = ipAddress;
			this.Port = port;
			base.ByteTransform = new ReverseBytesTransform();
			this.communicationLock = new object();
		}

		/// <inheritdoc cref="P:HslCommunication.Profinet.Siemens.SiemensPPI.Station" />
		// Token: 0x17000144 RID: 324
		// (get) Token: 0x0600063E RID: 1598 RVA: 0x0001DEE1 File Offset: 0x0001C0E1
		// (set) Token: 0x0600063F RID: 1599 RVA: 0x0001DEE9 File Offset: 0x0001C0E9
		public byte Station
		{
			get
			{
				return this.station;
			}
			set
			{
				this.station = value;
			}
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Siemens.SiemensPPI.Read(System.String,System.UInt16)" />
		// Token: 0x06000640 RID: 1600 RVA: 0x0001DEF4 File Offset: 0x0001C0F4
		[HslMqttApi("ReadByteArray", "")]
		public override OperateResult<byte[]> Read(string address, ushort length)
		{
			byte b = (byte)HslHelper.ExtractParameter(ref address, "s", (int)this.Station);
			OperateResult<byte[]> operateResult = SiemensPPIOverTcp.BuildReadCommand(b, address, length, false);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				object obj = this.communicationLock;
				lock (obj)
				{
					OperateResult<byte[]> operateResult2 = base.ReadFromCoreServer(operateResult.Content);
					bool flag3 = !operateResult2.IsSuccess;
					if (flag3)
					{
						result = operateResult2;
					}
					else
					{
						bool flag4 = operateResult2.Content[0] != 229;
						if (flag4)
						{
							result = new OperateResult<byte[]>("PLC Receive Check Failed:" + SoftBasic.ByteToHexString(operateResult2.Content, ' '));
						}
						else
						{
							OperateResult<byte[]> operateResult3 = base.ReadFromCoreServer(SiemensPPIOverTcp.GetExecuteConfirm(b));
							bool flag5 = !operateResult3.IsSuccess;
							if (flag5)
							{
								result = operateResult3;
							}
							else
							{
								OperateResult operateResult4 = SiemensPPIOverTcp.CheckResponse(operateResult3.Content);
								bool flag6 = !operateResult4.IsSuccess;
								if (flag6)
								{
									result = OperateResult.CreateFailedResult<byte[]>(operateResult4);
								}
								else
								{
									byte[] array = new byte[(int)length];
									bool flag7 = operateResult3.Content[21] == byte.MaxValue && operateResult3.Content[22] == 4;
									if (flag7)
									{
										Array.Copy(operateResult3.Content, 25, array, 0, (int)length);
									}
									result = OperateResult.CreateSuccessResult<byte[]>(array);
								}
							}
						}
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Siemens.SiemensPPI.ReadBool(System.String,System.UInt16)" />
		// Token: 0x06000641 RID: 1601 RVA: 0x0001E074 File Offset: 0x0001C274
		[HslMqttApi("ReadBoolArray", "")]
		public override OperateResult<bool[]> ReadBool(string address, ushort length)
		{
			byte b = (byte)HslHelper.ExtractParameter(ref address, "s", (int)this.Station);
			OperateResult<byte[]> operateResult = SiemensPPIOverTcp.BuildReadCommand(b, address, length, true);
			bool flag = !operateResult.IsSuccess;
			OperateResult<bool[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				object obj = this.communicationLock;
				lock (obj)
				{
					OperateResult<byte[]> operateResult2 = base.ReadFromCoreServer(operateResult.Content);
					bool flag3 = !operateResult2.IsSuccess;
					if (flag3)
					{
						result = OperateResult.CreateFailedResult<bool[]>(operateResult2);
					}
					else
					{
						bool flag4 = operateResult2.Content[0] != 229;
						if (flag4)
						{
							result = new OperateResult<bool[]>("PLC Receive Check Failed:" + SoftBasic.ByteToHexString(operateResult2.Content, ' '));
						}
						else
						{
							OperateResult<byte[]> operateResult3 = base.ReadFromCoreServer(SiemensPPIOverTcp.GetExecuteConfirm(b));
							bool flag5 = !operateResult3.IsSuccess;
							if (flag5)
							{
								result = OperateResult.CreateFailedResult<bool[]>(operateResult3);
							}
							else
							{
								OperateResult operateResult4 = SiemensPPIOverTcp.CheckResponse(operateResult3.Content);
								bool flag6 = !operateResult4.IsSuccess;
								if (flag6)
								{
									result = OperateResult.CreateFailedResult<bool[]>(operateResult4);
								}
								else
								{
									byte[] array = new byte[operateResult3.Content.Length - 27];
									bool flag7 = operateResult3.Content[21] == byte.MaxValue && operateResult3.Content[22] == 3;
									if (flag7)
									{
										Array.Copy(operateResult3.Content, 25, array, 0, array.Length);
									}
									result = OperateResult.CreateSuccessResult<bool[]>(SoftBasic.ByteToBoolArray(array, (int)length));
								}
							}
						}
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Siemens.SiemensPPI.Write(System.String,System.Byte[])" />
		// Token: 0x06000642 RID: 1602 RVA: 0x0001E218 File Offset: 0x0001C418
		[HslMqttApi("WriteByteArray", "")]
		public override OperateResult Write(string address, byte[] value)
		{
			byte b = (byte)HslHelper.ExtractParameter(ref address, "s", (int)this.Station);
			OperateResult<byte[]> operateResult = SiemensPPIOverTcp.BuildWriteCommand(b, address, value);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				object obj = this.communicationLock;
				lock (obj)
				{
					OperateResult<byte[]> operateResult2 = base.ReadFromCoreServer(operateResult.Content);
					bool flag3 = !operateResult2.IsSuccess;
					if (flag3)
					{
						result = operateResult2;
					}
					else
					{
						bool flag4 = operateResult2.Content[0] != 229;
						if (flag4)
						{
							result = new OperateResult<byte[]>("PLC Receive Check Failed:" + operateResult2.Content[0].ToString());
						}
						else
						{
							OperateResult<byte[]> operateResult3 = base.ReadFromCoreServer(SiemensPPIOverTcp.GetExecuteConfirm(b));
							bool flag5 = !operateResult3.IsSuccess;
							if (flag5)
							{
								result = operateResult3;
							}
							else
							{
								OperateResult operateResult4 = SiemensPPIOverTcp.CheckResponse(operateResult3.Content);
								bool flag6 = !operateResult4.IsSuccess;
								if (flag6)
								{
									result = operateResult4;
								}
								else
								{
									result = OperateResult.CreateSuccessResult();
								}
							}
						}
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Siemens.SiemensPPI.Write(System.String,System.Boolean[])" />
		// Token: 0x06000643 RID: 1603 RVA: 0x0001E33C File Offset: 0x0001C53C
		[HslMqttApi("WriteBoolArray", "")]
		public override OperateResult Write(string address, bool[] value)
		{
			byte b = (byte)HslHelper.ExtractParameter(ref address, "s", (int)this.Station);
			OperateResult<byte[]> operateResult = SiemensPPIOverTcp.BuildWriteCommand(b, address, value);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				object obj = this.communicationLock;
				lock (obj)
				{
					OperateResult<byte[]> operateResult2 = base.ReadFromCoreServer(operateResult.Content);
					bool flag3 = !operateResult2.IsSuccess;
					if (flag3)
					{
						result = operateResult2;
					}
					else
					{
						bool flag4 = operateResult2.Content[0] != 229;
						if (flag4)
						{
							result = new OperateResult<byte[]>("PLC Receive Check Failed:" + operateResult2.Content[0].ToString());
						}
						else
						{
							OperateResult<byte[]> operateResult3 = base.ReadFromCoreServer(SiemensPPIOverTcp.GetExecuteConfirm(b));
							bool flag5 = !operateResult3.IsSuccess;
							if (flag5)
							{
								result = operateResult3;
							}
							else
							{
								OperateResult operateResult4 = SiemensPPIOverTcp.CheckResponse(operateResult3.Content);
								bool flag6 = !operateResult4.IsSuccess;
								if (flag6)
								{
									result = operateResult4;
								}
								else
								{
									result = OperateResult.CreateSuccessResult();
								}
							}
						}
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Siemens.SiemensPPIOverTcp.Read(System.String,System.UInt16)" />
		// Token: 0x06000644 RID: 1604 RVA: 0x0001E460 File Offset: 0x0001C660
		[DebuggerStepThrough]
		public override Task<OperateResult<byte[]>> ReadAsync(string address, ushort length)
		{
			SiemensPPIOverTcp.<ReadAsync>d__9 <ReadAsync>d__ = new SiemensPPIOverTcp.<ReadAsync>d__9();
			<ReadAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
			<ReadAsync>d__.<>4__this = this;
			<ReadAsync>d__.address = address;
			<ReadAsync>d__.length = length;
			<ReadAsync>d__.<>1__state = -1;
			<ReadAsync>d__.<>t__builder.Start<SiemensPPIOverTcp.<ReadAsync>d__9>(ref <ReadAsync>d__);
			return <ReadAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Siemens.SiemensPPIOverTcp.ReadBool(System.String,System.UInt16)" />
		// Token: 0x06000645 RID: 1605 RVA: 0x0001E4B4 File Offset: 0x0001C6B4
		[DebuggerStepThrough]
		public override Task<OperateResult<bool[]>> ReadBoolAsync(string address, ushort length)
		{
			SiemensPPIOverTcp.<ReadBoolAsync>d__10 <ReadBoolAsync>d__ = new SiemensPPIOverTcp.<ReadBoolAsync>d__10();
			<ReadBoolAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<bool[]>>.Create();
			<ReadBoolAsync>d__.<>4__this = this;
			<ReadBoolAsync>d__.address = address;
			<ReadBoolAsync>d__.length = length;
			<ReadBoolAsync>d__.<>1__state = -1;
			<ReadBoolAsync>d__.<>t__builder.Start<SiemensPPIOverTcp.<ReadBoolAsync>d__10>(ref <ReadBoolAsync>d__);
			return <ReadBoolAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Siemens.SiemensPPIOverTcp.Write(System.String,System.Byte[])" />
		// Token: 0x06000646 RID: 1606 RVA: 0x0001E508 File Offset: 0x0001C708
		[DebuggerStepThrough]
		public override Task<OperateResult> WriteAsync(string address, byte[] value)
		{
			SiemensPPIOverTcp.<WriteAsync>d__11 <WriteAsync>d__ = new SiemensPPIOverTcp.<WriteAsync>d__11();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.value = value;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<SiemensPPIOverTcp.<WriteAsync>d__11>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Siemens.SiemensPPIOverTcp.Write(System.String,System.Boolean[])" />
		// Token: 0x06000647 RID: 1607 RVA: 0x0001E55C File Offset: 0x0001C75C
		[DebuggerStepThrough]
		public override Task<OperateResult> WriteAsync(string address, bool[] value)
		{
			SiemensPPIOverTcp.<WriteAsync>d__12 <WriteAsync>d__ = new SiemensPPIOverTcp.<WriteAsync>d__12();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.value = value;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<SiemensPPIOverTcp.<WriteAsync>d__12>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Siemens.SiemensPPI.ReadByte(System.String)" />
		// Token: 0x06000648 RID: 1608 RVA: 0x0001BA1E File Offset: 0x00019C1E
		[HslMqttApi("ReadByte", "")]
		public OperateResult<byte> ReadByte(string address)
		{
			return ByteTransformHelper.GetResultFromArray<byte>(this.Read(address, 1));
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Siemens.SiemensPPI.Write(System.String,System.Byte)" />
		// Token: 0x06000649 RID: 1609 RVA: 0x0001BA2D File Offset: 0x00019C2D
		[HslMqttApi("WriteByte", "")]
		public OperateResult Write(string address, byte value)
		{
			return this.Write(address, new byte[]
			{
				value
			});
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Siemens.SiemensPPIOverTcp.ReadByte(System.String)" />
		// Token: 0x0600064A RID: 1610 RVA: 0x0001E5B0 File Offset: 0x0001C7B0
		[DebuggerStepThrough]
		public Task<OperateResult<byte>> ReadByteAsync(string address)
		{
			SiemensPPIOverTcp.<ReadByteAsync>d__15 <ReadByteAsync>d__ = new SiemensPPIOverTcp.<ReadByteAsync>d__15();
			<ReadByteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte>>.Create();
			<ReadByteAsync>d__.<>4__this = this;
			<ReadByteAsync>d__.address = address;
			<ReadByteAsync>d__.<>1__state = -1;
			<ReadByteAsync>d__.<>t__builder.Start<SiemensPPIOverTcp.<ReadByteAsync>d__15>(ref <ReadByteAsync>d__);
			return <ReadByteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Siemens.SiemensPPIOverTcp.Write(System.String,System.Byte)" />
		// Token: 0x0600064B RID: 1611 RVA: 0x0001E5FC File Offset: 0x0001C7FC
		[DebuggerStepThrough]
		public Task<OperateResult> WriteAsync(string address, byte value)
		{
			SiemensPPIOverTcp.<WriteAsync>d__16 <WriteAsync>d__ = new SiemensPPIOverTcp.<WriteAsync>d__16();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.value = value;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<SiemensPPIOverTcp.<WriteAsync>d__16>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Siemens.SiemensPPI.Start(System.String)" />
		// Token: 0x0600064C RID: 1612 RVA: 0x0001E650 File Offset: 0x0001C850
		[HslMqttApi]
		public OperateResult Start(string parameter = "")
		{
			byte b = (byte)HslHelper.ExtractParameter(ref parameter, "s", (int)this.Station);
			byte[] array = new byte[]
			{
				104,
				33,
				33,
				104,
				0,
				0,
				108,
				50,
				1,
				0,
				0,
				0,
				0,
				0,
				20,
				0,
				0,
				40,
				0,
				0,
				0,
				0,
				0,
				0,
				253,
				0,
				0,
				9,
				80,
				95,
				80,
				82,
				79,
				71,
				82,
				65,
				77,
				170,
				22
			};
			array[4] = this.station;
			byte[] send = array;
			object obj = this.communicationLock;
			OperateResult result;
			lock (obj)
			{
				OperateResult<byte[]> operateResult = base.ReadFromCoreServer(send);
				bool flag2 = !operateResult.IsSuccess;
				if (flag2)
				{
					result = operateResult;
				}
				else
				{
					bool flag3 = operateResult.Content[0] != 229;
					if (flag3)
					{
						result = new OperateResult<byte[]>("PLC Receive Check Failed:" + operateResult.Content[0].ToString());
					}
					else
					{
						OperateResult<byte[]> operateResult2 = base.ReadFromCoreServer(SiemensPPIOverTcp.GetExecuteConfirm(b));
						bool flag4 = !operateResult2.IsSuccess;
						if (flag4)
						{
							result = operateResult2;
						}
						else
						{
							result = OperateResult.CreateSuccessResult();
						}
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Siemens.SiemensPPI.Stop(System.String)" />
		// Token: 0x0600064D RID: 1613 RVA: 0x0001E748 File Offset: 0x0001C948
		[HslMqttApi]
		public OperateResult Stop(string parameter = "")
		{
			byte b = (byte)HslHelper.ExtractParameter(ref parameter, "s", (int)this.Station);
			byte[] array = new byte[]
			{
				104,
				29,
				29,
				104,
				0,
				0,
				108,
				50,
				1,
				0,
				0,
				0,
				0,
				0,
				16,
				0,
				0,
				41,
				0,
				0,
				0,
				0,
				0,
				9,
				80,
				95,
				80,
				82,
				79,
				71,
				82,
				65,
				77,
				170,
				22
			};
			array[4] = this.station;
			byte[] send = array;
			object obj = this.communicationLock;
			OperateResult result;
			lock (obj)
			{
				OperateResult<byte[]> operateResult = base.ReadFromCoreServer(send);
				bool flag2 = !operateResult.IsSuccess;
				if (flag2)
				{
					result = operateResult;
				}
				else
				{
					bool flag3 = operateResult.Content[0] != 229;
					if (flag3)
					{
						result = new OperateResult<byte[]>("PLC Receive Check Failed:" + operateResult.Content[0].ToString());
					}
					else
					{
						OperateResult<byte[]> operateResult2 = base.ReadFromCoreServer(SiemensPPIOverTcp.GetExecuteConfirm(b));
						bool flag4 = !operateResult2.IsSuccess;
						if (flag4)
						{
							result = operateResult2;
						}
						else
						{
							result = OperateResult.CreateSuccessResult();
						}
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Siemens.SiemensPPI.Start(System.String)" />
		// Token: 0x0600064E RID: 1614 RVA: 0x0001E840 File Offset: 0x0001CA40
		[DebuggerStepThrough]
		public Task<OperateResult> StartAsync(string parameter = "")
		{
			SiemensPPIOverTcp.<StartAsync>d__19 <StartAsync>d__ = new SiemensPPIOverTcp.<StartAsync>d__19();
			<StartAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<StartAsync>d__.<>4__this = this;
			<StartAsync>d__.parameter = parameter;
			<StartAsync>d__.<>1__state = -1;
			<StartAsync>d__.<>t__builder.Start<SiemensPPIOverTcp.<StartAsync>d__19>(ref <StartAsync>d__);
			return <StartAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Siemens.SiemensPPI.Stop(System.String)" />
		// Token: 0x0600064F RID: 1615 RVA: 0x0001E88C File Offset: 0x0001CA8C
		[DebuggerStepThrough]
		public Task<OperateResult> StopAsync(string parameter = "")
		{
			SiemensPPIOverTcp.<StopAsync>d__20 <StopAsync>d__ = new SiemensPPIOverTcp.<StopAsync>d__20();
			<StopAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<StopAsync>d__.<>4__this = this;
			<StopAsync>d__.parameter = parameter;
			<StopAsync>d__.<>1__state = -1;
			<StopAsync>d__.<>t__builder.Start<SiemensPPIOverTcp.<StopAsync>d__20>(ref <StopAsync>d__);
			return <StopAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x06000650 RID: 1616 RVA: 0x0001E8D7 File Offset: 0x0001CAD7
		public override string ToString()
		{
			return string.Format("SiemensPPIOverTcp[{0}:{1}]", this.IpAddress, this.Port);
		}

		/// <summary>
		/// 解析数据地址，解析出地址类型，起始地址，DB块的地址<br />
		/// Parse data address, parse out address type, start address, db block address
		/// </summary>
		/// <param name="address">起始地址，例如M100，I0，Q0，V100 -&gt;
		/// Start address, such as M100,I0,Q0,V100</param>
		/// <returns>解析数据地址，解析出地址类型，起始地址，DB块的地址 -&gt;
		/// Parse data address, parse out address type, start address, db block address</returns>
		// Token: 0x06000651 RID: 1617 RVA: 0x0001E8F4 File Offset: 0x0001CAF4
		public static OperateResult<byte, int, ushort> AnalysisAddress(string address)
		{
			OperateResult<byte, int, ushort> operateResult = new OperateResult<byte, int, ushort>();
			try
			{
				operateResult.Content3 = 0;
				bool flag = address.Substring(0, 2) == "AI";
				if (flag)
				{
					operateResult.Content1 = 6;
					operateResult.Content2 = S7AddressData.CalculateAddressStarted(address.Substring(2), false);
				}
				else
				{
					bool flag2 = address.Substring(0, 2) == "AQ";
					if (flag2)
					{
						operateResult.Content1 = 7;
						operateResult.Content2 = S7AddressData.CalculateAddressStarted(address.Substring(2), false);
					}
					else
					{
						bool flag3 = address[0] == 'T';
						if (flag3)
						{
							operateResult.Content1 = 31;
							operateResult.Content2 = S7AddressData.CalculateAddressStarted(address.Substring(1), false);
						}
						else
						{
							bool flag4 = address[0] == 'C';
							if (flag4)
							{
								operateResult.Content1 = 30;
								operateResult.Content2 = S7AddressData.CalculateAddressStarted(address.Substring(1), false);
							}
							else
							{
								bool flag5 = address.Substring(0, 2) == "SM";
								if (flag5)
								{
									operateResult.Content1 = 5;
									operateResult.Content2 = S7AddressData.CalculateAddressStarted(address.Substring(2), false);
								}
								else
								{
									bool flag6 = address[0] == 'S';
									if (flag6)
									{
										operateResult.Content1 = 4;
										operateResult.Content2 = S7AddressData.CalculateAddressStarted(address.Substring(1), false);
									}
									else
									{
										bool flag7 = address[0] == 'I';
										if (flag7)
										{
											operateResult.Content1 = 129;
											operateResult.Content2 = S7AddressData.CalculateAddressStarted(address.Substring(1), false);
										}
										else
										{
											bool flag8 = address[0] == 'Q';
											if (flag8)
											{
												operateResult.Content1 = 130;
												operateResult.Content2 = S7AddressData.CalculateAddressStarted(address.Substring(1), false);
											}
											else
											{
												bool flag9 = address[0] == 'M';
												if (flag9)
												{
													operateResult.Content1 = 131;
													operateResult.Content2 = S7AddressData.CalculateAddressStarted(address.Substring(1), false);
												}
												else
												{
													bool flag10 = address[0] == 'D' || address.Substring(0, 2) == "DB";
													if (flag10)
													{
														operateResult.Content1 = 132;
														string[] array = address.Split(new char[]
														{
															'.'
														});
														bool flag11 = address[1] == 'B';
														if (flag11)
														{
															operateResult.Content3 = Convert.ToUInt16(array[0].Substring(2));
														}
														else
														{
															operateResult.Content3 = Convert.ToUInt16(array[0].Substring(1));
														}
														operateResult.Content2 = S7AddressData.CalculateAddressStarted(address.Substring(address.IndexOf('.') + 1), false);
													}
													else
													{
														bool flag12 = address[0] == 'V';
														if (!flag12)
														{
															operateResult.Message = StringResources.Language.NotSupportedDataType;
															operateResult.Content1 = 0;
															operateResult.Content2 = 0;
															operateResult.Content3 = 0;
															return operateResult;
														}
														operateResult.Content1 = 132;
														operateResult.Content3 = 1;
														operateResult.Content2 = S7AddressData.CalculateAddressStarted(address.Substring(1), false);
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				operateResult.Message = ex.Message;
				return operateResult;
			}
			operateResult.IsSuccess = true;
			return operateResult;
		}

		/// <summary>
		/// 生成一个读取字数据指令头的通用方法<br />
		/// A general method for generating a command header to read a Word data
		/// </summary>
		/// <param name="station">设备的站号信息 -&gt; Station number information for the device</param>
		/// <param name="address">起始地址，例如M100，I0，Q0，V100 -&gt;
		/// Start address, such as M100,I0,Q0,V100</param>
		/// <param name="length">读取数据长度 -&gt; Read Data length</param>
		/// <param name="isBit">是否为位读取</param>
		/// <returns>包含结果对象的报文 -&gt; Message containing the result object</returns>
		// Token: 0x06000652 RID: 1618 RVA: 0x0001EC60 File Offset: 0x0001CE60
		public static OperateResult<byte[]> BuildReadCommand(byte station, string address, ushort length, bool isBit)
		{
			OperateResult<byte, int, ushort> operateResult = SiemensPPIOverTcp.AnalysisAddress(address);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				byte[] array = new byte[33];
				array[0] = 104;
				array[1] = BitConverter.GetBytes(array.Length - 6)[0];
				array[2] = BitConverter.GetBytes(array.Length - 6)[0];
				array[3] = 104;
				array[4] = station;
				array[5] = 0;
				array[6] = 108;
				array[7] = 50;
				array[8] = 1;
				array[9] = 0;
				array[10] = 0;
				array[11] = 0;
				array[12] = 0;
				array[13] = 0;
				array[14] = 14;
				array[15] = 0;
				array[16] = 0;
				array[17] = 4;
				array[18] = 1;
				array[19] = 18;
				array[20] = 10;
				array[21] = 16;
				array[22] = (isBit ? 1 : 2);
				array[23] = 0;
				array[24] = BitConverter.GetBytes(length)[0];
				array[25] = BitConverter.GetBytes(length)[1];
				array[26] = (byte)operateResult.Content3;
				array[27] = operateResult.Content1;
				array[28] = BitConverter.GetBytes(operateResult.Content2)[2];
				array[29] = BitConverter.GetBytes(operateResult.Content2)[1];
				array[30] = BitConverter.GetBytes(operateResult.Content2)[0];
				int num = 0;
				for (int i = 4; i < 31; i++)
				{
					num += (int)array[i];
				}
				array[31] = BitConverter.GetBytes(num)[0];
				array[32] = 22;
				result = OperateResult.CreateSuccessResult<byte[]>(array);
			}
			return result;
		}

		/// <summary>
		/// 生成一个写入PLC数据信息的报文内容
		/// </summary>
		/// <param name="station">PLC的站号</param>
		/// <param name="address">地址</param>
		/// <param name="values">数据值</param>
		/// <returns>是否写入成功</returns>
		// Token: 0x06000653 RID: 1619 RVA: 0x0001EDCC File Offset: 0x0001CFCC
		public static OperateResult<byte[]> BuildWriteCommand(byte station, string address, byte[] values)
		{
			OperateResult<byte, int, ushort> operateResult = SiemensPPIOverTcp.AnalysisAddress(address);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				int num = values.Length;
				byte[] array = new byte[37 + values.Length];
				array[0] = 104;
				array[1] = BitConverter.GetBytes(array.Length - 6)[0];
				array[2] = BitConverter.GetBytes(array.Length - 6)[0];
				array[3] = 104;
				array[4] = station;
				array[5] = 0;
				array[6] = 124;
				array[7] = 50;
				array[8] = 1;
				array[9] = 0;
				array[10] = 0;
				array[11] = 0;
				array[12] = 0;
				array[13] = 0;
				array[14] = 14;
				array[15] = 0;
				array[16] = (byte)(values.Length + 4);
				array[17] = 5;
				array[18] = 1;
				array[19] = 18;
				array[20] = 10;
				array[21] = 16;
				array[22] = 2;
				array[23] = 0;
				array[24] = BitConverter.GetBytes(num)[0];
				array[25] = BitConverter.GetBytes(num)[1];
				array[26] = (byte)operateResult.Content3;
				array[27] = operateResult.Content1;
				array[28] = BitConverter.GetBytes(operateResult.Content2)[2];
				array[29] = BitConverter.GetBytes(operateResult.Content2)[1];
				array[30] = BitConverter.GetBytes(operateResult.Content2)[0];
				array[31] = 0;
				array[32] = 4;
				array[33] = BitConverter.GetBytes(num * 8)[1];
				array[34] = BitConverter.GetBytes(num * 8)[0];
				values.CopyTo(array, 35);
				int num2 = 0;
				for (int i = 4; i < array.Length - 2; i++)
				{
					num2 += (int)array[i];
				}
				array[array.Length - 2] = BitConverter.GetBytes(num2)[0];
				array[array.Length - 1] = 22;
				result = OperateResult.CreateSuccessResult<byte[]>(array);
			}
			return result;
		}

		/// <summary>
		/// 根据错误信息，获取到文本信息
		/// </summary>
		/// <param name="code">状态</param>
		/// <returns>消息文本</returns>
		// Token: 0x06000654 RID: 1620 RVA: 0x0001EF78 File Offset: 0x0001D178
		public static string GetMsgFromStatus(byte code)
		{
			switch (code)
			{
			case 1:
				return "Hardware fault";
			case 2:
			case 4:
				break;
			case 3:
				return "Illegal object access";
			case 5:
				return "Invalid address(incorrent variable address)";
			case 6:
				return "Data type is not supported";
			default:
				if (code == 10)
				{
					return "Object does not exist or length error";
				}
				if (code == 255)
				{
					return "No error";
				}
				break;
			}
			return StringResources.Language.UnknownError;
		}

		/// <summary>
		/// 根据错误信息，获取到文本信息
		/// </summary>
		/// <param name="errorClass">错误类型</param>
		/// <param name="errorCode">错误代码</param>
		/// <returns>错误信息</returns>
		// Token: 0x06000655 RID: 1621 RVA: 0x0001EFFC File Offset: 0x0001D1FC
		public static string GetMsgFromStatus(byte errorClass, byte errorCode)
		{
			bool flag = errorClass == 128 && errorCode == 1;
			string result;
			if (flag)
			{
				result = "Switch\u2002in\u2002wrong\u2002position\u2002for\u2002requested\u2002operation";
			}
			else
			{
				bool flag2 = errorClass == 129 && errorCode == 4;
				if (flag2)
				{
					result = "Miscellaneous\u2002structure\u2002error\u2002in\u2002command.\u2002\u2002Command is not supportedby CPU";
				}
				else
				{
					bool flag3 = errorClass == 132 && errorCode == 4;
					if (flag3)
					{
						result = "CPU is busy processing an upload or download CPU cannot process command because of system fault condition";
					}
					else
					{
						bool flag4 = errorClass == 133 && errorCode == 0;
						if (flag4)
						{
							result = "Length fields are not correct or do not agree with the amount of data received";
						}
						else
						{
							bool flag5 = errorClass == 210;
							if (flag5)
							{
								result = "Error in upload or download command";
							}
							else
							{
								bool flag6 = errorClass == 214;
								if (flag6)
								{
									result = "Protection error(password)";
								}
								else
								{
									bool flag7 = errorClass == 220 && errorCode == 1;
									if (flag7)
									{
										result = "Error in time-of-day clock data";
									}
									else
									{
										result = StringResources.Language.UnknownError;
									}
								}
							}
						}
					}
				}
			}
			return result;
		}

		/// <summary>
		/// 创建写入PLC的bool类型数据报文指令
		/// </summary>
		/// <param name="station">PLC的站号信息</param>
		/// <param name="address">地址信息</param>
		/// <param name="values">bool[]数据值</param>
		/// <returns>带有成功标识的结果对象</returns>
		// Token: 0x06000656 RID: 1622 RVA: 0x0001F0E0 File Offset: 0x0001D2E0
		public static OperateResult<byte[]> BuildWriteCommand(byte station, string address, bool[] values)
		{
			OperateResult<byte, int, ushort> operateResult = SiemensPPIOverTcp.AnalysisAddress(address);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				byte[] array = SoftBasic.BoolArrayToByte(values);
				byte[] array2 = new byte[37 + array.Length];
				array2[0] = 104;
				array2[1] = BitConverter.GetBytes(array2.Length - 6)[0];
				array2[2] = BitConverter.GetBytes(array2.Length - 6)[0];
				array2[3] = 104;
				array2[4] = station;
				array2[5] = 0;
				array2[6] = 124;
				array2[7] = 50;
				array2[8] = 1;
				array2[9] = 0;
				array2[10] = 0;
				array2[11] = 0;
				array2[12] = 0;
				array2[13] = 0;
				array2[14] = 14;
				array2[15] = 0;
				array2[16] = 5;
				array2[17] = 5;
				array2[18] = 1;
				array2[19] = 18;
				array2[20] = 10;
				array2[21] = 16;
				array2[22] = 1;
				array2[23] = 0;
				array2[24] = BitConverter.GetBytes(values.Length)[0];
				array2[25] = BitConverter.GetBytes(values.Length)[1];
				array2[26] = (byte)operateResult.Content3;
				array2[27] = operateResult.Content1;
				array2[28] = BitConverter.GetBytes(operateResult.Content2)[2];
				array2[29] = BitConverter.GetBytes(operateResult.Content2)[1];
				array2[30] = BitConverter.GetBytes(operateResult.Content2)[0];
				array2[31] = 0;
				array2[32] = 3;
				array2[33] = BitConverter.GetBytes(values.Length)[1];
				array2[34] = BitConverter.GetBytes(values.Length)[0];
				array.CopyTo(array2, 35);
				int num = 0;
				for (int i = 4; i < array2.Length - 2; i++)
				{
					num += (int)array2[i];
				}
				array2[array2.Length - 2] = BitConverter.GetBytes(num)[0];
				array2[array2.Length - 1] = 22;
				result = OperateResult.CreateSuccessResult<byte[]>(array2);
			}
			return result;
		}

		/// <summary>
		/// 检查西门子PLC的返回的数据和合法性，对反馈的数据进行初步的校验
		/// </summary>
		/// <param name="content">服务器返回的原始的数据内容</param>
		/// <returns>是否校验成功</returns>
		// Token: 0x06000657 RID: 1623 RVA: 0x0001F290 File Offset: 0x0001D490
		public static OperateResult CheckResponse(byte[] content)
		{
			bool flag = content.Length < 21;
			OperateResult result;
			if (flag)
			{
				result = new OperateResult(10000, "Failed, data too short:" + SoftBasic.ByteToHexString(content, ' '));
			}
			else
			{
				bool flag2 = content[17] != 0 || content[18] > 0;
				if (flag2)
				{
					result = new OperateResult((int)content[19], SiemensPPIOverTcp.GetMsgFromStatus(content[18], content[19]));
				}
				else
				{
					bool flag3 = content[21] != byte.MaxValue;
					if (flag3)
					{
						result = new OperateResult((int)content[21], SiemensPPIOverTcp.GetMsgFromStatus(content[21]));
					}
					else
					{
						result = OperateResult.CreateSuccessResult();
					}
				}
			}
			return result;
		}

		/// <summary>
		/// 根据站号信息获取命令二次确认的报文信息
		/// </summary>
		/// <param name="station">站号信息</param>
		/// <returns>二次命令确认的报文</returns>
		// Token: 0x06000658 RID: 1624 RVA: 0x0001F328 File Offset: 0x0001D528
		public static byte[] GetExecuteConfirm(byte station)
		{
			byte[] array = new byte[]
			{
				16,
				2,
				0,
				92,
				94,
				22
			};
			array[1] = station;
			int num = 0;
			for (int i = 1; i < 4; i++)
			{
				num += (int)array[i];
			}
			array[4] = (byte)num;
			return array;
		}

		// Token: 0x040001AB RID: 427
		private byte station = 2;

		// Token: 0x040001AC RID: 428
		private object communicationLock;
	}
}
